#include "RL_PMS9103M.h"



void PMS9103M::send(byte cmd,byte dataH,byte dataL) {
    Packet[2] = cmd;
    Packet[3] = dataH;
    Packet[4] = dataL;
    LRC = 0;
    foreach(Packet) {
        this->write(row);
        LRC += row;
    }
    this->write((LRC >> 8) & 0xff);
    this->write(LRC & 0xff);
}

void PMS9103M::sleep() {
    send(PMS_CMD_SLEEP, 0x00, PMS_CMD_SLEEP_SLEEP);
}

void PMS9103M::wakeUp() {
    send(PMS_CMD_SLEEP, 0x00, PMS_CMD_SLEEP_WAKEUP);
}
//状态切换  PASSIVE/ACTIVE
void PMS9103M::setMode(byte mode) {
    send(PMS_CMD_MODE,0x00,mode);
    while(this->available() > 0) {
        this->read();
    }
}

void PMS9103M::request() {
    send(PMS_CMD_READ,0x00,0x00); 
    delay(100);
}


PMS5003T_DATA* PMS9103M::readPms() {
    int bi = 0;
    unsigned char c;
    unsigned long pms_timeout = 0;
    int data_len = 0;
  
    //
    // Send request read
    //
   // this->write(PMS_CMD_REQUEST_READ, sizeof(PMS_CMD_REQUEST_READ));
    //delay(DelayValue[PMS9103M::AFTER_SEND_REQUEST_CMD]); // Wait for PMS5003 to calculate data.
  
    memset(&(Buff[0]), 0, 32);
  
    //
    // Search for the sign - {0x42, 0x4d}
    //
    pms_timeout = millis();
    while ((millis() - pms_timeout) < 1300) {
        if(this->available()) {
            Buff[1] = this->read();
            if((Buff[0] == 0x42) && (Buff[1] == 0x4d)) {
                bi = 2;
                break;
            }
            else {
                  Buff[0] = Buff[1];
            }
            delay(5); // PMS9103M is a slow sensor
        }
    }

    if(bi != 2) { // timeout
        LastErr = 1;
        Pd = 0;
        return Pd;
    }

    //
    // Receive data and copy to "Buff".
    //
    pms_timeout = millis();
    while ((millis() - pms_timeout) < 1500) {

        if(bi >= 32) break; // exceed buffer size
        if((data_len != 0) && (bi >= (data_len + 4))) break; // exceed data length

        if(this->available()) {
            c = this->read();
            Buff[bi] = c;
            bi++;
            
            if(bi == 4) {
                //
                // Buff[2] : high byte
                // Buff[3] : low byte
                //
                data_len = Buff[2];
                data_len = (data_len << 8) + Buff[3];
            }
      
            delay(5); // PMS9103M is a slow sensor
        }
    }
    
    if(data_len == 0) { // data error
        LastErr = 2;
        Pd = 0;
        return Pd;
    }
    else {
        if(bi < (data_len + 4)) { // timeout
            LastErr = 3;
            Pd = 0;
            return Pd;
        }
    }

    //
    // Exchange high low bytes
    //
    for(int i = 2; i < 32; i = i + 2) {
        uint8_t temp8 = Buff[i];
        Buff[i] = Buff[i + 1];
        Buff[i + 1] = temp8;
    }

    Pd = (PMS5003T_DATA*)(&(Buff[0]));

    //
    // Use bi to calculate checksum
    //
    bi = 0;
    for(int i = 0; i < (Pd->DATA_LENGTH + 4 - 2); i++) {
      bi += Buff[i];
    }
    
    
    
    if((Pd->SIG_1 == 0x42) && (Pd->SIG_2 == 0x4d)) {
        return Pd;
    }
    else {
        LastErr = 6;
        Pd = 0;
        return Pd;
    }
}



int PMS9103M::getData(int type)
{
    switch (type) {
    case 1:return Pd->PM_SP_UG_1_0;
    case 2:return Pd->PM_SP_UG_1_0;
    case 3:return Pd->PM_SP_UG_10_0;
    case 4:return Pd->PM_AE_UG_1_0;
    case 5:return Pd->PM_AE_UG_2_5;
    case 6:return Pd->PM_AE_UG_10_0;
    case 7:return Pd->PM_NUM_0_3;
    case 8:return Pd->PM_NUM_0_5;
    case 9:return Pd->PM_NUM_1_0;
    case 10:return Pd->PM_NUM_2_5;
    case 11:return Pd->PM_NUM_5_0;
    case 12:return Pd->PM_NUM_10_0;
    default:
        return -1; // 表示无效的类型
    }
}